package com.turing.data.scripts.contract.filter

import com.turing.data.engine.CombineLongText
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils

/**
 * 场景：资产管理合同
 * 版本：Version 1
 * 要素名称：是否为MOM/FOF产品
 */

class IfMomFof_30173_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        String REGEX_IS_MOM = /.*(为|属于).*MOM.*/

        String REGEX_IS_FOF = /.*(为|属于).*FOF.*/

        String REGEX_NOT_FOF = /.*不.*MOM.*/

        String REGEX_NOT_MOM = /.*不.*FOF.*/


        def REGEX_ALL = "本计划不属于基金中基金资产管理计划（ FOF）及管理人中管理人资产管理计划（MOM）"

        def textPathMap = dependParams.textPathMap
        def JSONTextMap = dependParams.JSONTextMap

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
//                    return  resultVOs
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def text = resultVOs[i].text
                def trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,40)
                printf "\n"
                println trueValue
                printf "\n"
                if ( trueValue =~ REGEX_NOT_FOF ||  trueValue =~ REGEX_NOT_MOM ){
                    def resultVO = resultVOs.get( i )
                    if( REGEX_ALL.contains( text ) ){
                        def expandOffset = 0
                        def start = resultVO.start
                        def end = resultVO.end
                        while( expandOffset < 45 &&  rawText.length() > end + expandOffset ){
                            def expandStr = rawText.substring(start,end + expandOffset)
                            if( expandStr =~ REGEX_ALL ){
                                def fileTextStr = ""
                                fileTextStr = JSONTextMap.get(resultVO.childTaskSn)
                                TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                                objectNew = TextUtils.correctPositionInfoByOffset(objectNew, 0, expandOffset, fileTextStr, rawText)
                                objectNew.text = rawText.substring( start , end + expandOffset  )
                                objectNew.businessValue = rawText.substring( start , end + expandOffset )
                                trueValueSet.add(objectNew)
                                return trueValueSet
                            }
                            expandOffset ++
                        }
                    }
                    trueValueSet.add( resultVO )
                    return trueValueSet
                }
            }
        }

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,15)

                if (trueValue =~ REGEX_IS_MOM ||  trueValue =~ REGEX_IS_FOF  ){
                    trueValueSet.add(resultVOs.get(i))
                    return trueValueSet
                }
            }
        }

        if(!CollectionUtils.isEmpty(resultVOs)&&resultVOs.size()>0){
            TaskElementVO.DrawResultVO DrawResultVO = new TaskElementVO.DrawResultVO()
            DrawResultVO.text=""
            trueValueSet.add(DrawResultVO)
            return trueValueSet
        }
        return  resultVOs

    }

}
